//
//  HDMenuView.m
//  HDecoration
//
//  Created by 曹群 on 15/8/13.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDMenuView.h"
@interface HDMenuView()
@property(nonatomic,copy)NSArray *titleArray;
@property(nonatomic,strong)UIImageView *selectedView;
@end
@implementation HDMenuView

- (id)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArray = titleArray;
        self.backgroundColor = [UIColor whiteColor];
        UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0.5)];
        topLine.image = [UIImage imageNamed:@"直线"];
        [self addSubview:topLine];
        UIImageView *bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height-0.5, self.width, 0.5)];
        bottomLine.image = [UIImage imageNamed:@"直线"];
        [self addSubview:bottomLine];
        [self loadBtns];
    }
    return self;
}

- (void)loadBtns
{
    for (int i=0; i<_titleArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*self.width/_titleArray.count, 0, self.width/_titleArray.count, self.height);
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitle:_titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor hexStringToColor:@"#282828"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor hexStringToColor:@"#e94929"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100+i;
        [self addSubview:btn];
        if (i) {
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.width/_titleArray.count, 10, 0.5, 20)];
            line.image = [UIImage imageNamed:@"竖线"];
            [self addSubview:line];
        }
    }
}

- (void)setSelectedIndex:(int)value
{
    _selectedIndex = value;
    UIButton *selectedBtn = (UIButton *)[self viewWithTag:100+_selectedIndex];
    selectedBtn.selected = YES;
    [self moveSelectedView:_selectedIndex];
    if ([self.delegate respondsToSelector:@selector(selectMenuIndex:)]) {
        [self.delegate selectMenuIndex:_selectedIndex];
    }
}

- (void)clickBtn:(UIButton *)sender
{
    self.selectedIndex = (int)sender.tag-100;
    for (int i=0; i<_titleArray.count; i++) {
        UIButton *btn = (UIButton *)[self viewWithTag:100+i];
        if (btn.tag!=sender.tag) {
            btn.selected = NO;
        }
    }
}

- (void)moveSelectedView:(int)selectedIndex
{
    if (!self.selectedView.superview) {
        [self addSubview:self.selectedView];
    }
    [UIView animateWithDuration:0.2 animations:^{
        self.selectedView.left = (self.width/_titleArray.count-57)/2.0+selectedIndex*self.width/_titleArray.count;
    }];
}

#pragma mark - getters
- (UIImageView *)selectedView
{
    if (!_selectedView) {
        _selectedView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width/_titleArray.count-57)/2.0, self.height-2, 57, 2)];
        _selectedView.image = [UIImage imageNamed:@"选中提示"];
    }
    return _selectedView;
}

@end
